package com.smart.thrift.discoveryandbalance.discovery;

import com.smart.thrift.constants.ThriftConstants;
import com.smart.thrift.discoveryandbalance.discovery.consul.ConsulRegistry;
import com.smart.thrift.discoveryandbalance.discovery.zookeeper.ZookeeperRegistry;

import javax.net.ssl.SSLContext;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;

public abstract class AbstractRegistry<T> implements Registry<T> {

    private static final String REGISTRY_URL_TEMPLATE = "http://%s:%d";

    private static RegistryProperties registryProperties;
    private static BaseAuthProperties baseAuthProperties;
    private static SSLContext sslContext;
    private static Properties clientProperties;

    protected AbstractRegistry() {
    }

    public static AbstractRegistry getInstance(RegistryProperties registryProperties,
                                               BaseAuthProperties baseAuthProperties,
                                               SSLContext sslContext,
                                               Properties clientProperties){
        AbstractRegistry.registryProperties = registryProperties;
        AbstractRegistry.baseAuthProperties = baseAuthProperties;
        AbstractRegistry.sslContext = sslContext;
        AbstractRegistry.clientProperties = clientProperties;
        if (ThriftConstants.DiscoveryType.ZOOKEEPER.equals(registryProperties.getDiscoveryType())) {
            return ZookeeperRegistry.getInstance();
        } else if (ThriftConstants.DiscoveryType.CONSUL.equals(registryProperties.getDiscoveryType())) {
            return ConsulRegistry.getInstance();
        }
        return null;
    }

    public static AbstractRegistry getInstance(RegistryProperties registryProperties,
                                               SSLContext sslContext,
                                               Properties clientProperties){
       return getInstance(registryProperties, new BaseAuthProperties(), sslContext, clientProperties);
    }

    public static AbstractRegistry getInstance(RegistryProperties registryProperties,
                                               Properties clientProperties) throws NoSuchAlgorithmException {
        return getInstance(registryProperties, SSLContext.getDefault(), clientProperties);
    }

    public static AbstractRegistry getInstance(RegistryProperties registryProperties) throws NoSuchAlgorithmException {
        return getInstance(registryProperties, new Properties());
    }

    public static AbstractRegistry getInstance(String registryType, String host, Integer port) throws NoSuchAlgorithmException {
        RegistryProperties registryProperties1 = new RegistryProperties();
        registryProperties1.setHost(host);
        registryProperties1.setPort(port);
        registryProperties1.setDiscoveryType(registryType);
        return getInstance(registryProperties1);
    }

    public static AbstractRegistry getInstance(String registryType, String connectString) throws NoSuchAlgorithmException {
        RegistryProperties registryProperties1 = new RegistryProperties();
        registryProperties1.setConnectString(connectString);
        registryProperties1.setDiscoveryType(registryType);
        return getInstance(registryProperties1);
    }

    public static AbstractRegistry getInstance(String host, Integer port) throws NoSuchAlgorithmException {
        return getInstance(ThriftConstants.DiscoveryType.DISCOVERY_DEFAULT, host, port);
    }

    protected RegistryProperties getRegistryProperties() {
        return registryProperties;
    }

    protected BaseAuthProperties getBaseAuthProperties() {
        return baseAuthProperties;
    }

    protected SSLContext getSslContext() {
        return sslContext;
    }

    protected Properties getClientProperties() {
        return clientProperties;
    }
}
